package com.hy.common;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;


/****
 * 自定义的一个loading等待框
 *
 * @author Eason
 */
public class UserLoadDialog extends Dialog {

    TextView loadingMsg;
    LinearLayout loadingLayout;

    private String messageText = "加载中...";

    public UserLoadDialog(Context context) {
        super(context, R.style.UserLoadDialog);
        initDialog();
    }

    public UserLoadDialog(Context context, int theme) {
        super(context, theme);
    }

    /**
     * 设置加载提示语
     *
     * @param message 提示语
     */
    public void setMessageText(String message) {
        messageText = message;
    }

    private void initDialog() {
        setContentView(R.layout.dialog_user_loading);
        loadingLayout = findViewById(R.id.loading_view);
        loadingMsg = findViewById(R.id.tv_tip);
        setContentView(loadingLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        setCancelable(true);
        getWindow().getAttributes().gravity = Gravity.CENTER;
        setCanceledOnTouchOutside(false);
//        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        if (!TextUtils.isEmpty(messageText)) {
            loadingMsg.setText(messageText);
        }
    }

    /**
     * setMessage 提示内容
     *
     * @param strMessage
     * @return
     */
    public UserLoadDialog setMessage(String strMessage) {
        if (TextUtils.isEmpty(strMessage)) {
            return this;
        }
        if (loadingMsg != null) {
            loadingMsg.setText(strMessage);
        }
        messageText = strMessage;
        return this;
    }

}
